﻿using System;
using UnityEngine;

namespace SG
{
    public class ObjectMgr : MgrCore
    {
        private static ObjectMgr m_instance;
        public static ObjectMgr Instance()
        {
            if (m_instance == null)
            {
                m_instance = new ObjectMgr();
            }
            return m_instance;
        }

        GameObject m_ObjectRoot = null;
        public Transform GetObjectRoot()
        {
            if (m_ObjectRoot == null)
            {
                m_ObjectRoot = new GameObject("ObjectRoot");
            }
            return m_ObjectRoot.transform;
        }

        GameObject m_BulletRoot;
        public Transform GetBulletRoot()
        {
            if (m_BulletRoot == null)
            {
                m_BulletRoot = new GameObject("BulletRoot");
                //Object.DontDestroyOnLoad(m_SkillRoot);
            }
            return m_BulletRoot.transform;
        }

        GameObject m_TempRoot;
        public Transform GetTempRoot()
        {
            if (m_TempRoot == null)
            {
                m_TempRoot = new GameObject("TempRoot");
                //Object.DontDestroyOnLoad(m_SkillRoot);
            }
            return m_TempRoot.transform;
        }

        public GameObject CreateObj(string strName, Transform parent = null)
        {
            var obj = Resources.Load<GameObject>(strName); ;
            //bool isDontDestory = ResourceTool.IsAssetDontDestory(strName);
            //var obj = ResourceLoader.Instance().LoadAsset<GameObject>(strName, isDontDestory);
            //if (obj == null)
            //{
            //   XKLog.LogWarning(string.Format( "读取profabs失败! 文件{0}不存在!", strName ) );
            //    return null;
            //}

            return GameObject.Instantiate(obj, parent);

        }
        public GameObject CreateObj(GameObject obj, Transform parent = null)
        {
            return GameObject.Instantiate(obj, parent);

        }
        public GameObject CreateNoDestoryObj(string strName, Transform parent = null)
        {
            var obj = ResourceLoader.Instance().LoadAsset<GameObject>(strName, true);
            if (obj == null)
            {
                XKLog.LogWarning(string.Format("读取profabs失败! 文件{0}不存在!", strName));
                return null;
            }
            return GameObject.Instantiate(obj);
        }

        //提供创建obj 实例化的快捷接口，ResourceLoader资源，然后实例化Instantiate
        public void CreateObjAysnc(string strName, Transform parent = null, Action<GameObject> _callBack = null)
        {
            ResourceLoader.Instance().LoadAssetAsync<GameObject>(strName, (obj) =>
            {
                if (obj != null)
                {
                    if (parent != null && !parent.Equals(null))
                    {
                        var gameObj = GameObject.Instantiate(obj, parent) as GameObject;
                        _callBack?.Invoke(gameObj);
                    }
                    else
                    {
                        var gameObj = GameObject.Instantiate(obj) as GameObject;
                        _callBack?.Invoke(gameObj);
                    }
                }
                else
                {
                    XKLog.LogWarning("创建prbfabs失败 = " + strName);
                    _callBack?.Invoke(null);
                }
            });
        }


        public AudioClip CreateAudioClip(string strName)
        {
            return ResourceLoader.Instance().LoadAsset<AudioClip>(strName);
        }
        public AudioClip CreateAudioClip(AudioClip clip)
        {
            return UnityEngine.Object.Instantiate(clip, GetTempRoot()) as AudioClip;
        }

        //提供创建obj的一个快捷方式
        public GameObject InstantiateObject(GameObject obj, Transform parent = null)
        {
            var gameObj = UnityEngine.Object.Instantiate(obj, parent);
            //gameObj.SetActive(true);
            return gameObj;
        }
    }
}